package T6_commite_update;
public class T6_commite_notifyAll extends Thread {    
    public static void main(String[] args) {    
    	 Calculator calculator = new Calculator();    
         new ReaderResult(calculator).start();    
         new ReaderResult(calculator).start();    
         new ReaderResult(calculator).start();    
         calculator.start(); 
    }    
} 
class ReaderResult extends Thread {    
    Calculator c;    
    public ReaderResult(Calculator c) {    
    	this.c = c;    
    }    
    public void run() {    
    	try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
    	synchronized (c) {    
    		try {   
    			if(c.flag){
    				System.out.println(Thread.currentThread() + "等待计算结果。。。");    
    				c.wait();  
    			}
    		} catch (InterruptedException e) {    
    			e.printStackTrace();    
    		}    
    		System.out.println(Thread.currentThread() + "计算结果为：" + c.total);    
    	}    
    }      
}  

class Calculator extends Thread {    
    int total;    
    public boolean flag = true ;
    public void run() {    
    	synchronized (this) {    
    		for (int i = 0; i < 101; i++) {    
    			total += i;    
    		}    
    		notify();  
    		flag = false;
    	}    
    }    
}
